import { get, post } from '../ajax'
import { getAjaxBaseUrl } from '../handler'
import type {
  WARNING_LEVELS,
  WARNING_STATES,
  WARNING_WORK_ORDER_STATES,
} from '@/enum'

import type {
  AjaxPagingResult,
  AlarmSaveOrUpdateWarningRuleParams,
  AlarmSaveOrUpdateWarningRuleParamsItem,
  MyWorkOrder,
  WarningInfo,
  WorkOrderFull,
  WorkOrderUser,
} from '@/types'

/** 根据指标id查询该指标告警规则 */
export function selectWarningRuleByIndicatorId(indicatorId: number) {
  return get<AlarmSaveOrUpdateWarningRuleParams>(
    `${controller}/selectWarningRuleByIndicatorId`,
    {
      indicatorId,
    },
  )
}

/** 新增或修改指标告警规则 */
export function saveOrUpdateWarningRule(params: Partial<AlarmSaveOrUpdateWarningRuleParamsItem>[]) {
  const params1 = (params || []).map((v) => {
    return {
      ...v,
      // 删除 monitorType，用 timeType 代替
      monitorType: undefined,
      timeType: v.monitorType,
    }
  })
  return post(`${controller}/saveOrUpdateWarningRule`, params1)
}

/** 批量解绑指标告警规则 */
export function deleteWarningRuleBatch(indicatorIdList: number[]) {
  return get(`${controller}/deleteWarningRuleBatch`, {
    indicatorIdList: indicatorIdList.join(','),
  })
}

const controller = getAjaxBaseUrl('/alarm', '/energy')

export interface SelectWarningPageParams {
  pageNumber: number
  pageSize: number
  type: '1' | '2'
  warningType?: WARNING_LEVELS
  state?: WARNING_STATES
  indicatorCode?: string
  workOrderNum?: string
}

/**
 * 分页查询告警信息（告警历史信息）
 * @field type 1-实时告警 2-告警历史
 */
export function selectWarningPage(data: SelectWarningPageParams) {
  return post<AjaxPagingResult<WarningInfo[]>>(
    `${controller}/selectWarningPage`,
    data,
  )
}

export interface SaveWarningWorkOrderParams {
  processingEndTime: string
  warningId: number
  warningWorkOrderUserList: WorkOrderUser[]
}

/**
 * 生成工单信息以及委派处理
 */
export function saveWarningWorkOrder(data: SaveWarningWorkOrderParams) {
  return post(`${controller}/saveWarningWorkOrder`, data)
}

export interface UpdateWarningByStateParams {
  auditContent: string
  id: number
  state: WARNING_STATES
}
/**
 * 修改告警状态（忽略）
 */
export function updateWarningByState(data: UpdateWarningByStateParams) {
  return post(`${controller}/updateWarningByState`, data)
}

/**
 * 根据工单号查询告警审核信息（含告警信息）
 */
export function selectWarningAuditingByWorkOrderNum(workOrderNum: string) {
  return get<WorkOrderFull>(
    `${controller}/selectWarningAuditingByWorkOrderNum`,
    {
      workOrderNum,
    },
  )
}

export interface SelectWarningWorkOrderByUserParams {
  pageNumber: number
  pageSize: number
  indicatorCode?: string
  warningType?: WARNING_LEVELS
  workOrderNum?: string
  workOrderState?: WARNING_WORK_ORDER_STATES
}

/**
 * 获取当前用户的工单信息（列表）
 */
export function selectWarningWorkOrderByUser(data: SelectWarningWorkOrderByUserParams) {
  return post<AjaxPagingResult<MyWorkOrder[]>>(
    `${controller}/selectWarningWorkOrderByUser`,
    data,
  )
}

export interface UpdateWorkOrderParams {
  id: number // workOrderId
  workOrderNum: string
  state: WARNING_WORK_ORDER_STATES
  auditContent?: string
  processingContent?: string
  processingTime?: string
}

/**
 * 修改工单信息（提交、审核工单）
 */
export function updateWorkOrder(data: UpdateWorkOrderParams) {
  return post(`${controller}/updateWorkOrder`, data)
}

/**
 * 导出实时告警、告警信息
 */
export function exportWarning(data: SelectWarningPageParams) {
  return post(`${controller}/exportWarning`, data)
}

/**
 * 导出告警工单
 */
export function exportWarningWorkOrder(data: SelectWarningWorkOrderByUserParams) {
  return post(`${controller}/exportWarningWorkOrder`, data)
}
